package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.mapper.StrategyMapper;
import cn.wolfcode.trip.base.query.CollectQueryObject;
import cn.wolfcode.trip.base.query.SearchQueryObject;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.service.IStrategyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StrategyServiceImpl implements IStrategyService{
    @Autowired
    private StrategyMapper strategyMapper;


    @Override
    public void saveOrUpdate(Strategy strategy) {
        if (strategy.getId() == null) {
            strategyMapper.insert(strategy);
        }else{
            strategyMapper.updateByPrimaryKey(strategy);
        }
    }

    @Override
    public PageInfo query(StrategyQueryObject qo) {
        //因为页面中默认查询第一页,当前页设置为2,会查询第二页数据,合理化分页,如果大于最大页数,不会保存还是会查出最大页的数据
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),true,false,false);
        PageHelper.orderBy(qo.getOrderBy());
        List<Strategy> list = strategyMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public List<Strategy> selectAll() {
        return strategyMapper.selectAll();
    }


    @Override
    public Strategy selectStrategyById(Long id) {
        return strategyMapper.selectByPrimaryKey(id);
    }

    /**
     * 通过关键字查询该攻略总数
     * @param keyword
     * @return
     */
    public int selectCountByKeyword(String keyword) {
        return strategyMapper.selectCountByKeyword(keyword);
    }

    /**
     * 通过关键字查询攻略
     * @param qo
     * @return
     */
    public PageInfo searchQuery(SearchQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Strategy> list = strategyMapper.selectByKeyword(qo);
        return new PageInfo(list);
    }

    /**
     * 查询用户收藏的攻略
     */
    public PageInfo selectForCollect(CollectQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),true,false,false);
        List<Strategy> list = strategyMapper.selectForCollect(qo);
        return new PageInfo(list);
    }

}
